<?php

/**
 * 访问量加1
 * @param  int  $id 文章id
 * @return bool
 */
function incVisits($id)
{
    return db('article')->where('id', $id)->setInc('visits', 1);
}
/**
 * 获取评论数
 *
 * @param  int $id
 * @return int
 */
function getCommentsNum($id)
{
    return model('Comment')->getCount(['aid'=>$id,'status'=>1]);
}
/**
 * 获取标签
 *
 * @param  string $string
 * @return string
 */
function getTag($string)
{
    $result="";
    $tagsInfo=explode(",", $string);
    foreach ($tagsInfo as $key => $value) {
        $tagId=(int)$value;
        $tagData=model('Tag')->getDataByWhere(['id'=>$tagId], '', '', true);
        $t=($tagId%2==0?"<a class=\"btn btn-white btn-xs tag\" href=".url('index/index/tag', ['flag'=>$tagData['flag']])."><i class=\"fa fa-tag\"></i>&nbsp;".$tagData['name']."</a>":"<a class=\"btn btn-info btn-xs tag\" href=".url('index/index/tag', ['flag'=>$tagData['flag']])."><i class=\"fa fa-tag\"></i>&nbsp;".$tagData['name']."</a>");
        $result.=$t;
    }
    return $result;
}
/**
 * 通过日期获取文章
 *
 * @param  int  $start
 * @return string
 */
function getArticlesByDate($start)
{
    $s=strtotime($start);
    $e=strtotime('+1 Month', $s);
    $articleList="";
    $list=db('article')
        ->where('status', 1)
        ->where('create_time', 'between time', [$s,$e])
        ->order('create_time DESC')
        ->select();
    foreach ($list as $value) {
        $l="<li class=\"list-group-item\"><a class=\"btn-link\" href=".url('index/index/article', ['id'=>$value['id']]).">".$value['title']."</a></li>";
        $articleList.=$l;
    }
    return $articleList;
}
/**
 * 通过id获取文章的上下篇
 * @param $id 文章ID
 * @param $pos 文章的方向 prev 上一篇 next 下一篇
 */
function getArticlePos($id, $pos)
{
    if (!$id) {
        return false;
    }
    if ($pos == 'prev') {
        $newid = model('Article')
            ->order('id ASC')
            ->limit(1)
            ->value('id');
        if ($id <= $newid) {
            return false;
        }
        $where['id'] = ['lt',$id];
        $where['status'] = ['eq',1];
        $data = model('Article')
            ->field('id,title')
            ->where($where)
            ->order('id DESC')
            ->limit(1)
            ->find();
        if ($data) {
            $link='<a href="'.url('index/index/article', ['id'=>$data['id']]).'" class="btn btn-primary btn-outline btn-block"><i class="fa fa-arrow-left"></i>&nbsp;'.$data['title'].'</a>';
        } else {
            $link='';
        }
        return $link;
    } elseif ($pos == 'next') {
        $newid = model('Article')
            ->order('id DESC')
            ->limit(1)
            ->value('id');
        if ($newid <= $id) {
            return false;
        }
        $where['id'] = ['gt',$id];
        $where['status'] = ['eq',1];
        $data = model('Article')
            ->field('id,title')
            ->where($where)
            ->order('id ASC')
            ->limit(1)
            ->find();
        if ($data) {
            $link='<a href="'.url('index/index/article', ['id'=>$data['id']]).'" class="btn btn-primary btn-outline btn-block">'.$data['title'].'&nbsp;<i class="fa fa-arrow-right"></i></a>';
        } else {
            $link='<a href="javascript:void(0)" class="btn btn-primary btn-outline btn-block">没有了</a>';
        }
        return $link;
    }
}
/**
 * 获取评论目录树
 *
 * @param int $aid
 * @param integer $parent_id
 * @param array $result
 * @return array
 */
function getCommentList($aid, $parent_id = 0, &$result = [])
{
    $arr = model('Comment')
            ->alias('c')
            ->field('c.*,ou.nickname,ou.avatar')
            ->join('__USER__ ou ', ' c.uid=ou.id')
            ->where(['c.parent_id'=>$parent_id,'c.aid'=>$aid,'c.status'=>1])
            ->order('c.create_time DESC')
            ->select();
    if (empty($arr)) {
        return [];
    }
    foreach ($arr as $cm) {
        $thisArr=&$result[];
        $cm['children'] = getCommentList($cm['aid'], $cm['id'], $thisArr);
        $thisArr = $cm;
    }
    return $result;
}
